package b2.net
{
	import b2.config.*;
	import b2.util.*;
	
	import flash.display.*;
	import flash.events.*;
	import flash.net.*;
	import flash.system.ApplicationDomain;

	public class ResourceHelper
	{
		/**
		 * 唯一实例
		 */
		static private var instance:ResourceHelper;
		/**
		 * 主资源域
		 */
		private var mainDomain:ApplicationDomain; 
		private var preludeDomain:ApplicationDomain;
		private var taskDomain:ApplicationDomain;
		private var festivalTaskDomain:ApplicationDomain; 
		private var onComplete:Function;
		private var onProgress:Function;
		
		//加载主资源的传输信息
		private var libraryBytesLoaded:uint; 
		private var libraryBytesTotal:uint; 
		private var libraryLoadFlag:Boolean;
		private var configFileBytesLoaded:uint; 
		private var configFileBytesTotal:uint; 
		private var configFileLoadFlag:Boolean;
		
		//加载任务资源的传输信息
		private var taskResourceBytesLoaded:uint; 
		private var taskResourceBytesTotal:uint; 
		private var taskResourceLoadFlag:Boolean;
		private var festivalTaskResourceBytesLoaded:uint; 
		private var festivalTaskResourceBytesTotal:uint; 
		private var festivalTaskResourceLoadFlag:Boolean;
		 
		public function ResourceHelper()
		{
		}
		
		static public function getInstance():ResourceHelper
		{
			if (!instance)
				instance = new ResourceHelper();
			return instance;
		}
	    
		/**
		 * 加载主资源
		 * 
		 * @param	onProgress	载入进度更新的回调函数
		 * @param	onComplete	载入完毕时的回调函数
		 */
		public function startLoadMainResource(onProgress:Function, onComplete:Function):void
		{
			this.onProgress = onProgress;
			this.onComplete = onComplete;
			//加载主图形库资源
			var resourceLoader:Loader = new Loader();
			resourceLoader.load(new URLRequest(Profile.URL_MainResource));
			resourceLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onMainResourceLoaded);
			resourceLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onMainResourceProgress);
			//加载游戏参数设定文件
			var configFileLoader:URLLoader = new URLLoader();
			configFileLoader.load(new URLRequest(Profile.URL_ConfigFile));
			configFileLoader.addEventListener(Event.COMPLETE, onConfigFileLoaded);
			configFileLoader.addEventListener(ProgressEvent.PROGRESS, onConfigFileProgress);
		}
		
		//加载片头动画
		public function startLoadPreludeResource(onProgress:Function, onComplete:Function):void
		{
			this.onProgress = onProgress;
			this.onComplete = onComplete;
			var resourceLoader:Loader = new Loader();
			resourceLoader.load(new URLRequest(Profile.URL_PreludeResource));
			resourceLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onPreludeResourceLoaded);
			resourceLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onPreludeResourceProgress);
		}
		
	    //加载任务资源
		public function startLoadTaskResource(onProgress:Function, onComplete:Function):void
		{
			this.onProgress = onProgress;
			this.onComplete = onComplete;
			//加载主线任务图形库资源
//			var me:PlayerInfo = PlayerInfoHelper.getInstance().players[PlayerInfoHelper.getInstance().userId];
//			var taskConfig:TaskConfig = Config.taskConfigs[me.accomplishedTaskId + 1];
//			var taskResourceLoader:Loader = new Loader();
//			taskResourceLoader.load(new URLRequest(Profile.URL_Root + taskConfig.resourceURL));
//			taskResourceLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onTaskResourceLoaded);
//			taskResourceLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onTaskResourceProgress);
//			//加载节日任务图形库资源
//			var festivalTaskConfig:TaskConfig = Config.festivalTaskConfigs[me.accomplishedFestivalTaskId + 1];
//			var festivalTaskResourceLoader:Loader = new Loader();
//			festivalTaskResourceLoader.load(new URLRequest(Profile.URL_Root + festivalTaskConfig.resourceURL));
//			festivalTaskResourceLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onFestivalTaskResourceLoaded);
//			festivalTaskResourceLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onFestivalTaskResourceProgress);
		}
		
	    private function onPreludeResourceLoaded(event:Event):void
	    {
		    this.preludeDomain = event.target.applicationDomain;
	    	this.onComplete();
	    }
	    
	    private function onPreludeResourceProgress(event:ProgressEvent):void
	    {
		    this.onProgress(event.bytesLoaded / event.bytesTotal);
	    }

	    public function getClassByName(name:String):Class
	    {
	    	var FetchClass:Class = getClassByNameFromMainResource(name);
	    	if (FetchClass == null)
	    	{
	    		FetchClass = getClassByNameFromTaskResource(name);
	    		if (FetchClass == null)
	    		{
	    			FetchClass = getClassByNameFromFestivalTaskResource(name);
			    	if (FetchClass == null)
			    		FetchClass = getClassByNameFromPreludeResource(name);
	    		}
	    	}
	    	return FetchClass;
	    }
	    
	    public function getClassByNameFromMainResource(name:String):Class
	    {
	    	if (mainDomain)
	    	{
	    		try
	    		{
	    			return mainDomain.getDefinition(name) as Class;
	    		}
	    		catch (error:Error)
	    		{
	    		}
    			return null;
	    	}
	    	else
	    	{
	    		return null;
	    	}
	    }
	    
	    public function getClassByNameFromPreludeResource(name:String):Class
	    {
	    	if (preludeDomain)
	    	{
	    		try
	    		{
	    			return preludeDomain.getDefinition(name) as Class;
	    		}
	    		catch (error:Error)
	    		{
	    		}
    			return null;
	    	}
	    	else
	    	{
	    		return null;
	    	}
	    }
	    
	    public function getClassByNameFromTaskResource(name:String):Class
	    {
	    	if (taskDomain)
	    	{
	    		try
	    		{
	    			return taskDomain.getDefinition(name) as Class;
	    		}
	    		catch (error:Error)
	    		{
	    		}
    			return null;
	    	}
	    	else
	    	{
	    		return null;
	    	}
	    }
	    
	    public function getClassByNameFromFestivalTaskResource(name:String):Class
	    {
	    	if (festivalTaskDomain)
	    	{
	    		try
	    		{
	    			return festivalTaskDomain.getDefinition(name) as Class;
	    		}
	    		catch (error:Error)
	    		{
	    		}
    			return null;
	    	}
	    	else
	    	{
	    		return null;
	    	}
	    }
	    
	    //加载任务资源过程响应
	    private function onTaskResourceProgress(event:ProgressEvent):void
	    {
	    	taskResourceBytesLoaded = event.bytesLoaded;
	    	taskResourceBytesTotal = event.bytesTotal;
			var percentage:Number = (taskResourceBytesLoaded + festivalTaskResourceBytesLoaded) / (taskResourceBytesTotal + festivalTaskResourceBytesTotal);
		    this.onProgress(percentage);
	    }

	    //加载任务资源完毕响应
	    private function onTaskResourceLoaded(event:Event):void
	    {
		    this.taskDomain = event.target.applicationDomain;
		    taskResourceLoadFlag = true;
		    if (taskResourceLoadFlag && festivalTaskResourceLoadFlag)
		    {
		    	this.onComplete();
		    }
	    }

	    //加载节日任务资源过程响应
	    private function onFestivalTaskResourceProgress(event:ProgressEvent):void
	    {
	    	festivalTaskResourceBytesLoaded = event.bytesLoaded;
	    	festivalTaskResourceBytesTotal = event.bytesTotal;
			var percentage:Number = (taskResourceBytesLoaded + festivalTaskResourceBytesLoaded) / (taskResourceBytesTotal + festivalTaskResourceBytesTotal);
		    this.onProgress(percentage);
	    }

	    //加载节日任务资源完毕响应
	    private function onFestivalTaskResourceLoaded(event:Event):void
	    {
		    this.festivalTaskDomain = event.target.applicationDomain;
		    festivalTaskResourceLoadFlag = true;
		    if (taskResourceLoadFlag && festivalTaskResourceLoadFlag)
		    {
		    	this.onComplete();
		    }
	    }

	    //加载主资源过程响应
	    private function onMainResourceProgress(event:ProgressEvent):void
	    {
	    	libraryBytesLoaded = event.bytesLoaded;
	    	libraryBytesTotal = event.bytesTotal;
			var percentage:Number = (libraryBytesLoaded + configFileBytesLoaded) / (libraryBytesTotal + configFileBytesTotal);
		    this.onProgress(percentage);
	    }

	    //加载主资源完毕响应
	    private function onMainResourceLoaded(event:Event):void
	    {
		    this.mainDomain = event.target.applicationDomain;
		    libraryLoadFlag = true;
		    if (libraryLoadFlag && configFileLoadFlag)
		    {
		    	this.onComplete();
		    }
	    }

	    private function onConfigFileProgress(event:ProgressEvent):void
	    {
	    	configFileBytesLoaded = event.bytesLoaded;
	    	configFileBytesTotal = event.bytesTotal;
			var percentage:Number = (libraryBytesLoaded + configFileBytesLoaded) / (libraryBytesTotal + configFileBytesTotal);
		    this.onProgress(percentage);
	    }
	    
	    private function onConfigFileLoaded(event:Event):void
	    {
	    	configFileLoadFlag = true;
	    	//解析游戏参数
			var dataXML:XML = XML(event.target.data);
			Config.parseXML(dataXML);
		    if (libraryLoadFlag && configFileLoadFlag)
		    {
		    	this.onComplete();
		    }
	    }

	}
}